<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
if(!$_G['uid']) {
	showmessage('not_loggedin', NULL, array(), array('login' => 1));
}
if(!$_G['cache']['plugin']['yydjourney']) {
    showmessage('yyd_friendliness:requirederror');
};
require_once './source/plugin/yyd_friendliness/require/function.php';

$config = $_G['cache']['plugin']['yyd_friendliness'];
$uid = $_GET['uid'] ? addslashes(strip_tags($_GET['uid'])) : $_G['uid'];
$user = C::t('common_member')->fetch($uid);
$css['halfwidth'] = $config['pgwidth'] /2;
//等级
$levels = array();
$lines = explode("\n", $config['lvname']);
foreach($lines as $key=>$value) {
    $s = explode("|", $value);
    $level['min'] = intval($s[0]);
    $level['name'] = dhtmlspecialchars($s[1]);
    $level['image'] = dhtmlspecialchars($s[2]);
    $levels[$key] = $level;
}

$results = list_user_friendiness(intval($uid), intval($config['addcredit']));
$friends = parse_friends($results);
//好感列表
if (!$_GET['uid']) {
    $results = rank_global_friendiness(intval($config['addcredit']), 0, 6);
    $tops = parse_friends($results);
    include template('yyd_friendliness:list');
} else {
    include template('yyd_friendliness:ajax_list');
}

function parse_friends($results) {
    global $levels, $config;
    $friends = array();
    foreach($results as $result) {
        foreach($levels as $key => $value) {
            if(intval($result['friendliness']) > $value['min']) {
                $result['level'] = $key;
            }
        }
        $result['nextlv'] = $levels[intval($result['level'] + 1)] ? $result['level'] + 1 : $result['level'];
        $progress = (intval($result['friendliness']) - $levels[$result['level']]['min']) / ($levels[$result['nextlv']]['min'] - $levels[$result['level']]['min']);
        $result['progress'] = (1 - $progress) * $config['pgheight'];
        $result['progress'] = round($result['progress'], 0, PHP_ROUND_HALF_DOWN);
        $friends[] = $result;
    }
    return $friends;
}